import scrapy
from ..items import DoubanItem


class AppSpider(scrapy.Spider):
    name = "app"
    allowed_domains = ["book.douban.com"]
    start_urls = ["https://book.douban.com/latest"]

    def parse(self, response):
        books = response.xpath('//div[@id="content"]//ul[@class="chart-dashed-list"]/li')
        for book in books:
            # 获取首页书籍链接
            link = book.xpath(".//a/@href").get()
            # 网络请求文章详情方法
            yield scrapy.Request(url=link, callback=self.parse_detail)

        # 获取下一页请求链接
        nextLink = response.xpath('//div[@id="content"]//div[@class="paginator"]//span[@class="next"]//a/@href').get()
        # 递归请求数据
        if nextLink is not None:
            nextLink = response.urljoin(nextLink)
            print('下一条link:', nextLink)
            yield scrapy.Request(url=nextLink, callback=self.parse)

    def parse_detail(self, response):
        items = DoubanItem()
        items["title"] = response.xpath('//*[@id="wrapper"]/h1/span/text()').get()
        items["author"] = response.xpath('//*[@id="info"]/span[1]/a/text()').get()
        yield items
